package com.winbomb.kingcard.base;

import java.util.ArrayList;
import java.util.List;

import com.winbomb.kingcard.IAttackRoute;
import com.winbomb.kingcard.IGameLogic;
import com.winbomb.kingcard.IPlayer;

/**
 * 进攻路线的实现类
 * 
 * @author winbomb
 * 
 */

public class AttackRoute implements IAttackRoute {

	public static final int ATK_RIGHT = 0;
	public static final int ATK_LEFT = 1;

	private List<IPlayer> atkRoute;
	private int currLocIndex;
	private int currTargetIndex;
	private boolean isClockwise;

	/**
	 * 构造函数
	 * 
	 * @param game
	 * @param battle
	 * @param atkDirection
	 *            进攻的方向
	 */
	public AttackRoute(IGameLogic game, IPlayer player, boolean isClockwise) {

		this.atkRoute = new ArrayList<IPlayer>();

		// attacker占据零位
		IPlayer currLoc = player;
		IPlayer target = player.getAttackTarget();
		this.atkRoute.add(currLoc);

		do {
			if (!isClockwise) {
				currLoc = currLoc.getRightPlayer();
			} else {
				currLoc = currLoc.getLeftPlayer();
			}

			this.atkRoute.add(currLoc);
		} while (currLoc != target && atkRoute.size() < game.getPlayerCount());

		this.currLocIndex = 0;
		this.currTargetIndex = 1;
		this.isClockwise = isClockwise;
	}

	@Override
	public IPlayer[] getAttackRoute() {
		return atkRoute.toArray(new IPlayer[atkRoute.size()]);
	}

	@Override
	public IPlayer getAttacker() {
		return atkRoute.get(0);
	}

	@Override
	public boolean isAtkClockwise() {
		return this.isClockwise;
	}

	@Override
	public IPlayer getCurrLocation() {
		return atkRoute.get(currLocIndex);
	}

	@Override
	public IPlayer getCurrTarget() {
		return atkRoute.get(currTargetIndex);
	}

	@Override
	public IPlayer getDefender() {
		return atkRoute.get(atkRoute.size() - 1);
	}

	@Override
	public void move() {
		if (currTargetIndex + 1 < atkRoute.size()) {
			currLocIndex++;
			currTargetIndex++;
		}
	}

}
